package kpetruk.tanks2d.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kpetruk.tanks2d.network.CommonNetwork;
import kpetruk.tanks2d.network.netobjects.ChatMessage;
import kpetruk.tanks2d.network.netobjects.RRBasicString;
import kpetruk.tanks2d.network.netobjects.RRPlayerName;
import kpetruk.tanks2d.network.netobjects.RequestResponce;
import kpetruk.tanks2d.server.GamePlayer;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.FrameworkMessage.KeepAlive;

public class GameClient {
  Logger log = LoggerFactory.getLogger("kpetruk.tanks2d.client.GameClient");
  CommonNetwork net = new CommonNetwork();
  //from server messages
  private LinkedList<RequestResponce> inputQueue = new LinkedList<RequestResponce>();
  //to server messages
  private LinkedList<RequestResponce> outputQueue = new LinkedList<RequestResponce>();
  private HashMap<Integer,GamePlayer> gamePlayers = new HashMap<Integer,GamePlayer>();

  
  
  public boolean init() {
    
    Client client = new Client();
    client.start();
    try {
      client.connect(5000, "127.0.0.1", CommonNetwork.tcpPort, CommonNetwork.udpPort);
    } catch (IOException e) {
      log.error("can't connect to server",e);
    }
    
    
    net.init(client);

    
    client.addListener(new Listener() {
      public void received (Connection connection, Object object) {
        if (object instanceof KeepAlive){
          log.debug("keepalive");
        }else if (object instanceof RequestResponce) {
          inputQueue.add((RequestResponce) object);
        }
      }
    });
    
    RRBasicString request = new RRBasicString();
    request.text = "TCP request test";
    client.sendTCP(request);
    request.text = "UDP request test";
    client.sendUDP(request);
    
    
    RRPlayerName sendName = new RRPlayerName();
    sendName.name = "Player"+ (new Random()).nextInt();
    log.debug("sending name: " + sendName.name);
    client.sendTCP(sendName);
    
    return true;
  }
  
  public void handleInput(){
    while (true && !inputQueue.isEmpty()) {
      Object rr = inputQueue.poll();
      if (rr instanceof ChatMessage) {
        ChatMessage cm = (ChatMessage)rr;
        log.info("[Chat] "+cm.message);
      }
    }
  }
  
  public void run(){
    while(true){
      //get input
      
      //NETWORKING
      //send input queue
      
      //get data from server
      handleInput();
      
      
      //OUTPUT
      //graphics
      //log.info("run: draw graphics");
      
      //chat
      
      
      //sound
    }
  }
  
  public static void main(String[] args) {
    GameClient gc = new GameClient();
    if (gc.init()) {
      gc.run();
    }

  }
}
